﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class AllSRMaintainRecords : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindProduct();
            }
        }

        private void BindProduct()
        {
            ApplicationProductService service = new ApplicationProductService();
            productGridView.DataSource = service.GetSRProductByKeyWord(keyWordTextBox.Text);
            service.GetProductByKeyWord(keyWordTextBox.Text);
            productGridView.DataBind();
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindProduct();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = string.Empty;
            BindProduct();
        }

        protected void maintainProductGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            if (e.NewPageIndex >= 0 && e.NewPageIndex < productGridView.PageCount)
            {
                productGridView.PageIndex = e.NewPageIndex;
            }
            
            BindProduct();
        }

        protected void viewLinkButton_Click(object sender, EventArgs e)
        {
            hfProductID.Value = ((LinkButton)sender).CommandArgument;
            maintainRecordModalPopupExtender.Show();
            BindMaintainRecord(long.Parse(hfProductID.Value));
        }

        private void BindMaintainRecord(long p)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(p);
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }
            if (!product.QuatationForm.IsLoaded)
            {
                product.QuatationForm.Load();
            }
            if (!product.ProblemAnalyzeReference.IsLoaded)
            {
                product.ProblemAnalyzeReference.Load();
            }
            rmaMaintainLabel.Text = product.ApplicationForm.ApplicationCode;
            typeLabel.Text = product.ProductType.ProductTypeName;
            serialLabel.Text = product.ProductSerialNumber;
            wxwcrqLabel.Text = product.RepairCompleteDateTime.Value.ToString("yyyy-MM-dd");
            blxxTextBox.Text = product.ProductProblemDescription;
            blyyDropDownList.SelectedValue = product.ReasonToProblem;
            blljTextBox.Text = product.RepairAccessory;
            ljwzTextBox.Text = product.AccessoryPlace;
            if (product.ChangeType != null)
            {
                if (product.ChangeType.EndsWith("更换"))
                {
                    ghRadioButton.Checked = true;
                }
                else if (product.ChangeType.EndsWith("替换"))
                {
                    thRadioButton.Checked = true;
                }
                thSerialNumberTextBox.Text = product.ReplaceProductSerialNumber;
            }
            double totalCost = 0;
            foreach (QuatationForm quatation in product.QuatationForm)
            {
                totalCost += quatation.Cost;
            }
            bjLabel.Text = totalCost.ToString();
            bzTextBox.Text = product.Remark;
            BindAnalyze(product.ProblemAnalyze);
        }

        private void BindAnalyze(ProblemAnalyze problemAnalyze)
        {
            if (problemAnalyze != null)
            {
                if (!string.IsNullOrEmpty(problemAnalyze.A))
                {
                    switch (problemAnalyze.A)
                    {
                        case "有缺陷":
                            RadioButton1.Checked = true;
                            break;
                        case "正常损坏":
                            RadioButton2.Checked = true;
                            break;
                        case "使用不当":
                            RadioButton3.Checked = true;
                            break;
                        case "非人为(不可抗力等)":
                            RadioButton4.Checked = true;
                            break;
                    }
                }

                if (!string.IsNullOrEmpty(problemAnalyze.B))
                {
                    switch (problemAnalyze.B)
                    {
                        case "环境不适":
                            RadioButton5.Checked = true;
                            break;
                        case "操作不当":
                            RadioButton6.Checked = true;
                            break;
                        case "正常损坏":
                            RadioButton7.Checked = true;
                            break;
                    }
                }

                if (!string.IsNullOrEmpty(problemAnalyze.C))
                {
                    switch (problemAnalyze.C)
                    {
                        case "固件Bug":
                            RadioButton8.Checked = true;
                            break;
                        case "正常升级":
                            RadioButton9.Checked = true;
                            break;
                    }
                }

                if (!string.IsNullOrEmpty(problemAnalyze.D))
                {
                    if (problemAnalyze.D.Equals("RestestOK"))
                    {
                        RadioButton10.Checked = true;
                    }
                }

                if (!string.IsNullOrEmpty(problemAnalyze.E))
                {
                    if (problemAnalyze.E.Equals("其它原因"))
                    {
                        RadioButton11.Checked = true;
                    }
                }
            }
        }

        protected void saveMaintainLinkButton_Click(object sender, EventArgs e)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(hfProductID.Value));
            if (!product.ProblemAnalyzeReference.IsLoaded)
            {
                product.ProblemAnalyzeReference.Load();
            }
            if (product.ProblemAnalyze == null)
            {
                product.ProblemAnalyze = new ProblemAnalyze();
            }

            product.ProblemAnalyze.A = getProblemAnalyzeA();
            product.ProblemAnalyze.B = getProblemAnalyzeB();
            product.ProblemAnalyze.C = getProblemAnalyzeC();
            product.ProblemAnalyze.D = getProblemAnalyzeD();
            product.ProblemAnalyze.E = getProblemAnalyzeE();

            product.ProductAppearance = blxxTextBox.Text;
            product.ReasonToProblem = blyyDropDownList.Text;
            product.RepairAccessory = blljTextBox.Text;
            product.AccessoryPlace = ljwzTextBox.Text;
            product.ChangeType = GetChangeType();
            if (thRadioButton.Checked)
            {
                product.ReplaceProductSerialNumber = thSerialNumberTextBox.Text;
            }

            service.UpdateProduct(product);
        }

        private string GetChangeType()
        {
            string changeType = string.Empty;
            if (thRadioButton.Checked)
            {
                changeType = thRadioButton.Text;
            }
            else if (ghRadioButton.Checked)
            {
                changeType = ghRadioButton.Text;
            }

            return changeType;
        }

        #region for problem analyze

        private string getProblemAnalyzeE()
        {
            string eStr = string.Empty;
            if (RadioButton11.Checked)
            {
                eStr = RadioButton11.Text;
            }
            return eStr;
        }

        private string getProblemAnalyzeD()
        {
            string dStr = string.Empty;
            if (RadioButton10.Checked)
            {
                dStr = RadioButton10.Text;
            }

            return dStr;
        }

        private string getProblemAnalyzeC()
        {
            string cStr = string.Empty;
            if (RadioButton9.Checked)
            {
                cStr = RadioButton9.Text;
            }
            else if (RadioButton8.Checked)
            {
                cStr = RadioButton8.Text;
            }

            return cStr;
        }

        private string getProblemAnalyzeB()
        {
            string bStr = string.Empty;
            if (RadioButton7.Checked)
            {
                bStr = RadioButton7.Text;
            }
            else if (RadioButton6.Checked)
            {
                bStr = RadioButton6.Text;
            }
            else if (RadioButton5.Checked)
            {
                bStr = RadioButton5.Text;
            }

            return bStr;
        }

        private string getProblemAnalyzeA()
        {
            string aStr = string.Empty;
            if (RadioButton4.Checked)
            {
                aStr = RadioButton4.Text;
            }
            else if (RadioButton3.Checked)
            {
                aStr = RadioButton3.Text;
            }
            else if (RadioButton2.Checked)
            {
                aStr = RadioButton2.Text;
            }
            else if (RadioButton1.Checked)
            {
                aStr = RadioButton1.Text;
            }
            return aStr;
        }

        #endregion

        //protected void productGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.DataItem != null)
        //    {
        //        ApplicationProduct product = (ApplicationProduct)e.Row.DataItem;
        //        if (!product.ApplicationFormReference.IsLoaded)
        //        {
        //            product.ApplicationFormReference.Load();
        //        }
        //        if (!product.ProductTypeReference.IsLoaded)
        //        {
        //            product.ProductTypeReference.Load();
        //        }
        //        if (!product.ProblemTypeReference.IsLoaded)
        //        {
        //            product.ProblemTypeReference.Load();
        //        }
        //    }
        //}
    }
}
